<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Xcx\Model;

use Think\Model\RelationModel;
use Xcx\Model\UsersModel;

/**
 * Class AccountingModel
 * @package Home\Model
 */
class AccountantModel extends RelationModel {

    protected $tableName = 'accountant_users';

    public function __construct() {
        parent::__construct();
        $this->usersModel = new UsersModel();
        $this->user_id = session('user_id');
    }

    public function getList($condition = '',$page = 1,$limit = 6,$cac_count = false) {
        $where = '  ';
        if($condition){
            $where .= $condition;
        }

        $select = M('accountant_users')
            ->where($where);
        if($cac_count){
            return $select->count();
        }
        $select = $select->page($page,$limit)->select();
        if($select){
            $arr = array();
            foreach ($select as &$find) {
                unset($find['password']);
                $temp['accountant_id'] = $find['au_id'];
                $temp['class_name_list'] = getCatName($find['cat_ids']);
                $temp['accept_num'] = $find['adoption_num'];
                $temp['respondent_name'] = $find['username'];
                $temp['work_unit'] = $find['work_unit'];
                $temp['reg_time'] = date('Y-m-d H:i:s', $find['add_time']);
                $temp['respondent_head'] = (strpos($find['head_pic'],'http') === false && $find['head_pic']) ? SITE_URL.$find['head_pic'] : $find['head_pic'];

                $arr[] = $temp;
            }
        }else{
            return false;
        }
        return $arr;
    }


    public function getInfoById($au_id = '') {
        $au_id = $au_id ? $au_id : 0;
        $find =  M('accountant_users')->where('au_id = '.$au_id.' and status = 1')->find();
        if($find){
            unset($find['password']);
        }
        return $find;
    }


    public function hasAdoption($question_id,$au_id) {
        $question_id = $question_id ? $question_id : 0;
        $au_id = $au_id ? $au_id : 0;
        if(!$question_id || !$au_id){
            return false;
        }
        $id = M('adoption')
            ->where('question_id = '.$question_id.' and accountant_id = '.$au_id.' and user_id = '.$this->user_id)
            ->getField('id');
        return $id;
    }


    public function hasCollect($question_id) {
        $question_id = $question_id ? $question_id : 0;
        if(!$question_id){
            return false;
        }
        $id = M('collect')
            ->where('question_id = '.$question_id.'  and user_id = '.$this->user_id)
            ->getField('id');
        return $id;
    }

}
